<template>
  <a-modal
    title="创建新代理"
    :visible="visible"
    @ok="onSubmit"
    @cancel="visible = false"
  >
    <a-form-model
      ref="ruleForm"
      :model="form"
      :rules="rules"
      :label-col="{ span: 6 }"
      :wrapper-col="{ span: 14 }"
    >
      <a-form-model-item ref="username" label="用户名" prop="username">
        <a-input
          v-model="form.username"
          @blur="
            () => {
              $refs.username.onFieldBlur()
            }
          "
        />
      </a-form-model-item>

      <a-form-model-item label="密码">
        <a-input default-value="88888888" disabled />
      </a-form-model-item>

      <a-form-model-item ref="fee_ratio" label="买币佣金比" prop="fee_ratio">
        <a-input-number
          v-model="form.fee_ratio"
          :max="maxFeeRatio"
          :min="0"
          :step="0.001"
          @blur="
            () => {
              $refs.fee_ratio.onFieldBlur()
            }
          "
        />
      </a-form-model-item>

      <a-form-model-item ref="tip" label="单笔奖励" prop="tip">
        <a-input-number
          v-model="form.tip"
          :max="maxTip"
          :min="0"
          @blur="
            () => {
              $refs.tip.onFieldBlur()
            }
          "
        />
      </a-form-model-item>
    </a-form-model>
  </a-modal>
</template>
<script>
import { mapGetters } from 'vuex'

export default {
  data() {
    return {
      visible: false,
      maxFeeRatio: 0,
      maxTip: 0,
      form: {
        username: '',
        fee_ratio: 0,
        tip: 0,
      },
      rules: {
        username: [
          {
            required: true,
            message: 'Please input Activity username',
            trigger: 'blur',
          },
          {
            min: 6,
            message: 'Length should more than 6',
            trigger: 'blur',
          },
        ],
        fee_ratio: [
          {
            required: true,
            message: 'Please input fee_ratio',
            trigger: 'blur',
          },
        ],
        tip: [
          {
            required: true,
            message: 'Please input tip',
            trigger: 'blur',
          },
        ],
      },
    }
  },
  computed: {
    ...mapGetters('account', ['sys']),
  },
  methods: {
    show() {
      //   console.log(this.sys)
      this.form.fee_ratio = this.sys.global_fee_ratio
      this.form.tip = this.sys.global_tip
      this.maxFeeRatio = parseFloat(this.form.fee_ratio)
      this.maxTip = parseInt(this.form.tip)
      this.visible = true
    },
    onSubmit() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          //   console.log(this.form)
          this.axios.post('/agencies', this.form).then((res) => {
            this.visible = false
            this.$message.success('创建成功')
            this.$emit('refresh')
          })
          //   alert('submit!')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
  },
}
</script>
